package com.engine.hrm.cmd.permissiontoadjust;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;
import java.util.Map.Entry;

/**
 * 功能权限表单
 * @author lvyi
 *
 */

public class GetPermissionToAdjustFormCmd extends AbstractCommonCommand<Map<String, Object>>{
	
	public GetPermissionToAdjustFormCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		List<Map<String, Object>> grouplist = new ArrayList<Map<String, Object>>();
		Map<String, Object> groupitem = null;
		List<Object> itemlist = null;
		RecordSet rs = new RecordSet();
		String sql = "";
		try {
			 User user = new User(1);
			 rs.writeLog(">>>>>GetPermissionToAdjustFormCmd>>>>>user="+user.getLoginid());
			if(!HrmUserVarify.checkUserRight("HrmRrightTransfer:Tran",user)) {
		  	retmap.put("status", "-1");
		  	retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
		  	return retmap;
		  }
			rs.writeLog(">>>>>GetPermissionToAdjustFormCmd>>>>>params="+params);
			//权限项类型、权限项、权限级别
			Map<String, List<HrmFieldBean>> fieldGroups= new LinkedHashMap<String, List<HrmFieldBean>>();
			List<HrmFieldBean> lsField = new ArrayList<HrmFieldBean>();
			HrmFieldBean hrmFieldBean = null;
			List<SearchConditionOption> selectOption = null;

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("transferType");
			hrmFieldBean.setFieldlabel("440");
			hrmFieldBean.setFieldhtmltype("5");
			hrmFieldBean.setType("1");
			selectOption = new ArrayList<SearchConditionOption>();
			selectOption.add(new SearchConditionOption("resource",SystemEnv.getHtmlLabelName(1867, user.getLanguage()),true));
			selectOption.add(new SearchConditionOption("department",SystemEnv.getHtmlLabelName(124, user.getLanguage())));
			selectOption.add(new SearchConditionOption("subcompany",SystemEnv.getHtmlLabelName(141, user.getLanguage())));
			selectOption.add(new SearchConditionOption("jobtitle",SystemEnv.getHtmlLabelName(6086, user.getLanguage())));
			selectOption.add(new SearchConditionOption("role",SystemEnv.getHtmlLabelName(122, user.getLanguage())));
			hrmFieldBean.setSelectOption(selectOption);
			lsField.add(hrmFieldBean);
			
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("resourceid");
			hrmFieldBean.setFieldlabel("1867");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("1");
			hrmFieldBean.setViewAttr(3);
			lsField.add(hrmFieldBean);
			
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("departmentid");
			hrmFieldBean.setFieldlabel("124");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("4");
			hrmFieldBean.setViewAttr(3);
			lsField.add(hrmFieldBean);
			
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("subcompanyid");
			hrmFieldBean.setFieldlabel("141");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("164");
			hrmFieldBean.setViewAttr(3);
			lsField.add(hrmFieldBean);
			
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("jobtitleid");
			hrmFieldBean.setFieldlabel("6086");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("24");
			hrmFieldBean.setViewAttr(3);
			lsField.add(hrmFieldBean);
			
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("roleid");
			hrmFieldBean.setFieldlabel("122");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("267");
			hrmFieldBean.setViewAttr(3);
			lsField.add(hrmFieldBean);
			
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("authorityTag");
			hrmFieldBean.setFieldlabel("63");
			hrmFieldBean.setFieldhtmltype("5");
			hrmFieldBean.setType("1");
			selectOption = new ArrayList<SearchConditionOption>();
			selectOption.add(new SearchConditionOption("transfer",SystemEnv.getHtmlLabelName(80, user.getLanguage()),true));
			selectOption.add(new SearchConditionOption("copy",SystemEnv.getHtmlLabelName(77, user.getLanguage())));
			selectOption.add(new SearchConditionOption("delete",SystemEnv.getHtmlLabelName(91, user.getLanguage())));
			hrmFieldBean.setSelectOption(selectOption);
			lsField.add(hrmFieldBean);
			
			fieldGroups.put("1361", lsField);
			
			HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
			SearchConditionItem searchConditionItem = null;
			Iterator<Entry<String, List<HrmFieldBean>>> iter = fieldGroups.entrySet().iterator();
			while (iter.hasNext()) {
				Entry<String, List<HrmFieldBean>> entry = iter.next();
				String grouplabel = entry.getKey();
				List<HrmFieldBean> fields = entry.getValue();
				groupitem = new HashMap<String, Object>();
				groupitem.put("title", SystemEnv.getHtmlLabelNames(grouplabel, user.getLanguage()));
				groupitem.put("defaultshow", true);
				
				itemlist = new ArrayList<Object>();
				for (int j = 0; j < fields.size(); j++) {
					hrmFieldBean = fields.get(j);
					searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
					if (hrmFieldBean.getFieldname().equalsIgnoreCase("roleId")) {
						searchConditionItem.getBrowserConditionParam().getDataParams().put("canShowSanyuan", "false");
						searchConditionItem.getBrowserConditionParam().getDestDataParams().put("canShowSanyuan", "false");
						searchConditionItem.getBrowserConditionParam().getCompleteParams().put("canShowSanyuan", "false");
					}
					itemlist.add(searchConditionItem);
				}
				groupitem.put("items", itemlist);
				grouplist.add(groupitem);
			}
			retmap.put("status", "1");
			retmap.put("formField", grouplist);
		} catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}
	
	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
